/**
 * 
 */
package com.humanmonth.framework.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;

/**
 * @author 林子龙
 * 
 */
public class ServletUtil {
	public static String XML_ENCODING = "text/xml;charset=UTF-8";

	public static void downloadToClient(HttpServletResponse response,byte[] data,String name) throws IOException{
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ new String(name.getBytes("gbk"), "iso8859-1") + "\"");	
		ServletOutputStream out = response.getOutputStream();
		try{
			out.write(data);
		}finally{
			out.close();
		}
	}
	/**
	 * 设置HTML header的文本类型及编码为"text/xml;charset=gb2312";
	 */
	public static void setXMLContentType(HttpServletResponse response) {
		response.setContentType(XML_ENCODING);
	}

	/**
	 * 从request读取多个值而不去除值头尾的空格，例如checkBox
	 */
	public static String[] getMultiValue(HttpServletRequest request, String paramName) {
		String[] values = request.getParameterValues(paramName);
		if (null == values) {
			return null;
		}
		for (int i = 0; i < values.length; i++) {
			values[i] = values[i];
		}
		return values;
	}

	/**
	 * 从request读取多个值并去附多个值，例如checkBox
	 */
	public static String[] removeMultiSpace(HttpServletRequest request, String paramName) {
		String[] values = request.getParameterValues(paramName);
		if (null == values) {
			return null;
		}
		for (int i = 0; i < values.length; i++) {
			values[i] = values[i].trim();
		}
		return values;
	}

	/**
	 * 获取checkbox的值，并转化为List
	 */
	public static List<String> getCheckboxValues(HttpServletRequest request, String paramName) {
		String[] values = request.getParameterValues(paramName);
		List<String> result = new LinkedList<String>();
		if (null == values) {
			return null;
		}
		for (int i = 0; i < values.length; i++) {
			result.add(values[i].trim());
		}
		return result;
	}

	/**
	 * 获取整型参数(带异常)
	 */
	public static Integer getIntegerParameterWithException(HttpServletRequest request, String parameterName) {
		String param = request.getParameter(parameterName);
		if (null == param || "".equals(param)) {
			throw new RuntimeException("获取不到参数:" + parameterName);
		} else {
			return Integer.valueOf(param);
		}
	}

	/**
	 * 获取整型参数，不带异常
	 */
	public static Integer getIntegerParameterWithNoException(HttpServletRequest request, String parameterName) {
		try {
			return ServletUtil.getIntegerParameterWithException(request, parameterName);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取参数,若参数不为空,即去空格
	 */
	public static String getStringParameterWithTrim(HttpServletRequest request, String paramName) {
		String result = request.getParameter(paramName);
		if (null != result) {
			return StringEscapeUtils.escapeSql(result.trim());
		} else {
			return result;
		}
	}

	/**
	 * 设置页面不没缓存
	 */
	public static void setNoCache(HttpServletResponse response) {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
	}

	/**
	 * 用javascript提示相关信息，并跳转到指定页面
	 * 
	 * @param msg提示信息
	 * @param url跳转到的路径
	 */
	public final static void outTips(HttpServletResponse response, String msg, String url) throws IOException {
		StringBuffer tips = new StringBuffer();
		tips.append("<script language='javascript'>");
		if (msg != null) {
			tips.append("alert('" + msg.replaceAll("\\\'", "\\\\\'") + "');");
		}
		if (url != null) {
			tips.append("window.location.replace('" + url + "');");
		} else {
			tips.append("window.history.back();");
		}
		tips.append("</script>");
		ServletUtil.outView(response, tips.toString());
	}

	/**
	 * 用javascript提示相关信息，并跳转到指定页面
	 * 
	 * @param msg提示信息
	 * @param url跳转到的路径
	 */
	public final static void outParentTips(HttpServletResponse response, String msg, String url) throws IOException {
		StringBuffer tips = new StringBuffer();
		tips.append("<script language='javascript'>");
		if (msg != null) {
			tips.append("alert('" + msg.replaceAll("\\\'", "\\\\\'") + "');");
		}
		if (url != null) {
			tips.append("parent.window.location.replace('" + url + "');");
		} else {
			tips.append("parent.window.history.back();");
		}
		tips.append("</script>");
		ServletUtil.outView(response, tips.toString());
	}

	/**
	 * 输出流到客户端
	 */
	public static void outView(HttpServletResponse response, Object ems) throws IOException {
		PrintWriter out = response.getWriter();
		out.print(ems);
		out.flush();
		out.close();
		out = null;
		return;
	}

}
